//
//  XZ_SelectView.m
//  Project_Demo
//
//  Created by caoxiaozhu on 2018/7/13.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

#import "XZ_SelectView.h"
#import "UIImage+Category.h"

@interface XZ_SelectView()
@property(nonatomic,strong)UIButton *tagBtn;
@property(nonatomic,strong)UIButton *selectedBtn;
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)UIButton *backBtn;
@property(nonatomic,strong)NSArray *contentBtnArr;
@property(nonatomic,strong)NSMutableDictionary *rememberContentBtnDict;
@property(nonatomic,assign)NSInteger selectedTag;
@property(nonatomic,assign)NSInteger selectedContenBtnTag;
@end

@implementation XZ_SelectView


-(instancetype)initWithFrame:(CGRect)frame BackGroundColor:(UIColor *)color titleArr:(NSArray *)titleArr ContentArr:(NSArray *)contentArr{
    
    if (self) {
        self = [super initWithFrame:frame];
        //上下两个分割线
        
//        UIView *topLineView = [UIView lineViewWithTop:YES Position:0 Color:Color_Line_Color];
//        [self addSubview:topLineView];
        
        UIView *bottomLineView = [UIView lineViewWithTop:false Position:frame.size.height Color:Color_Line_Color];
        [self addSubview:bottomLineView];
        
        self.backgroundColor = color;
        _contentBtnArr = contentArr;
        _rememberContentBtnDict = [NSMutableDictionary dictionary];
        [self setupTitleView:titleArr.count TitleArr:titleArr];
    };
    return self;
}


#pragma mark - 设置背景和内容View
- (UIView *)contentView{
    if (_contentView) {
        _contentView = [[UIView alloc]init];
        
    }
    return _contentView;
}


#pragma mark - 设置title
-(void)setupTitleView:(NSInteger)tagNum TitleArr:(NSArray *)titleArr{

    CGFloat btnW  = kScreenWidth / tagNum;
    CGFloat btnH = self.height;
    for (int i = 0 ; i<titleArr.count; i++) {
        _tagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _tagBtn.frame = CGRectMake(i * btnW, 0, btnW, btnH);
        [_tagBtn.layer masksToBounds];
        [_tagBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        [_tagBtn setTitleColor:Color_Black forState:UIControlStateNormal];
        _tagBtn.titleLabel.font = FONT(15);
        _tagBtn.tag = i;
        [_tagBtn setImage:[UIImage imageNamed:@"上箭头"] forState:UIControlStateNormal];
        _tagBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_tagBtn setImagePosition:LXMImagePositionRight spacing:15];
        [_tagBtn setImage:[UIImage imageNamed:@"下箭头"] forState:UIControlStateSelected];
        [_tagBtn addTarget:self action:@selector(clickTagAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_tagBtn];
    }
}


-(void)clickTagAction:(UIButton *)btn{
    
     [_contentView removeFromSuperview];
     [_backBtn removeFromSuperview];
    
    for (id view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *sender = (UIButton *)view;
            if (sender == btn) {
                btn.selected = !btn.selected;
                if (btn.selected) {
                    [self showSelectedView:btn];
                }else{
                    [self hideSelectedView:btn];
                }
            }else{
                sender.selected = false;
            }
        }
    }
}

#pragma mark - 显示视图
-(void)showSelectedView:(UIButton *)tagBtn{
//    NSLog(@"显示视图==>%ld",tag);
    [self setBackViewAndContentView:tagBtn];
}

#pragma mark - 隐藏视图
-(void)hideSelectedView:(UIButton *)tagBtn{
//    NSLog(@"隐藏视图==>%ld",tag);
    [self hide];
}


-(void)setBackViewAndContentView:(UIButton *)tagBtn{
    
    _selectedBtn = tagBtn;
    _backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, naviHeight+self.height, kScreenWidth, kScreenHeight)];
    _backBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    [_backBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self.superview addSubview:_backBtn];
    
    //添加手势
//    UITapGestureRecognizer *hideTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedAndHide)];
//    [_backView addGestureRecognizer:hideTap];
    
    _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, naviHeight + self.height, kScreenWidth, kScreenHeight)];
    _contentView.backgroundColor = Color_White;
    [self.superview insertSubview:_contentView atIndex:99];
    
    CGFloat margin = 15;
    CGFloat btnH = 50;
    CGFloat btnW = (kScreenWidth - 4 *margin) / 3;
//    CGFloat btnW = 50;
//    CGFloat margin = (kscreenWidth - (50 * 3)) / (3 + 1);
    
    //分配每个数组在内容里
    NSInteger count = ((NSArray *)_contentBtnArr[_selectedBtn.tag]).count;
    
    for (int i = 0; i< count; i++) {
        NSInteger row = i / 3;
        NSInteger col = i % 3;
        UIButton *contentBtn = [[UIButton alloc]init];
        contentBtn.frame = CGRectMake(margin + (btnW + margin) * col, margin + (btnH + margin) * row, btnW, btnH);
        contentBtn.tag = i;
//        contentBtn.backgroundColor = XZRandomColor;
        contentBtn.layer.borderWidth = 1;
        contentBtn.layer.borderColor = Color_Black.CGColor;
        contentBtn.layer.masksToBounds = true;
        
        [contentBtn setTitle:_contentBtnArr[_selectedBtn.tag][i] forState:UIControlStateNormal];
        [contentBtn setTitleColor:Color_Black forState:UIControlStateNormal];
        [contentBtn setBackgroundColor:Color_Red forState:UIControlStateSelected];
        

        //读取存储字典里的值
        NSString *selected = [NSString stringWithFormat:@"%ld",(long)_selectedBtn.tag];
        NSString *value = [_rememberContentBtnDict valueForKey:selected];
//        NSLog(@"value===>%@",value);
        
        if (contentBtn.tag == [value integerValue]) {
            [contentBtn setSelected:true];
        }
        
        [contentBtn addTarget:self action:@selector(contentBtnSelectedAndHide:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:contentBtn];
        
        if (i == count - 1) {
            _contentView.height = 0;
            [UIView animateWithDuration:0.2 animations:^{
                self->_contentView.height = contentBtn.maxY + margin;
            }];
        }
    }
}


-(void)contentBtnSelectedAndHide:(UIButton *)btn{
    
//    NSLog(@"selectetTag==>%ld,tagBtnName==>%ld",(long)_selectedBtn.tag,(long)btn.tag);
    btn.selected = true;
    _selectedTag = _selectedBtn.tag;
    _selectedContenBtnTag = btn.tag;
    
    NSString *selectedTag = [NSString stringWithFormat:@"%ld",(long)_selectedTag];
    NSString *selectedContenBtnTag = [NSString stringWithFormat:@"%ld",(long)_selectedContenBtnTag];
    [_rememberContentBtnDict setValue:selectedContenBtnTag forKey:selectedTag];
    
//    NSString *btnTag = [NSString stringWithFormat:@"%ld",(long)btn.tag];
//    [[NSUserDefaults standardUserDefaults]setObject:btnTag forKey:@"selectedContenBtnTag"];
    
    
    for (id view in _contentView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *btnOther = (UIButton *)view;
            if (btnOther != btn) {
                btnOther.selected = false;
            }
        }
    }
    
    [self hide];
}

-(void)hide{
    _selectedBtn.selected = false;
    [UIView animateWithDuration:0.2 animations:^{
        
        self->_contentView.height = 0;
        //遍历所有按钮
        for (id view in self->_contentView.subviews) {
            if ([view isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)view;
                [btn removeFromSuperview];
            }
        }
        
        self->_backBtn.alpha = 0;
        [self.superview insertSubview:self->_backBtn atIndex:1];
    }];
}

@end
